package com.hongxun.modules.bss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BMaterialEntity;
import com.hongxun.modules.api.service.BMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.APWireEntity;
import com.hongxun.modules.bss.service.APWireService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目导线选型表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-03 13:46:03
 */
@RestController
@RequestMapping("/bss/apwire")
public class APWireController {
	@Autowired
	private APWireService aPWireService;
	@Autowired
	private BMaterialService bMaterialService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_w_datastate != '删除' ";
		}
        query.put("filterSql",wheres+" ");
		List<APWireEntity> aPWireList = aPWireService.queryList(query);
		int total = aPWireService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPWireList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		APWireEntity aPWire = aPWireService.queryObject(guid);
		
		return R.ok().put("aPWire", aPWire);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody APWireEntity aPWire){
		aPWireService.save(aPWire);
		
		return R.ok();
	}
	@RequestMapping("/saveselect/{guid}")
	public R saveselect(@RequestBody String[] guids,@PathVariable("guid") String guid){
		//aPWireService.save(aPWire);
		for(String one :guids ) {
			Map<String, Object> params = new HashMap<String, Object>();
			String wheres = " 1=1 ";
			wheres += " and  p_guid = '" + guid + "' ";

			wheres += " and  p_m_guid = '" + one + "' ";

			params.put("filterSql", wheres + " ");
			List<APWireEntity> aPWireList = aPWireService.queryList(params);

			if (aPWireList.size() == 0) {

				BMaterialEntity bMaterialEntity = bMaterialService.queryObject(one);
				APWireEntity aPWireEntity = new APWireEntity();
				aPWireEntity.setPGuid(guid);
				aPWireEntity.setPMGuid(one);
				aPWireEntity.setPWDatastate("新增");
				aPWireEntity.setPWName(bMaterialEntity.getMName());
				//节假日修改
				aPWireEntity.setMbanktype(bMaterialEntity.getMbanktype());
				aPWireEntity.setWiretype(bMaterialEntity.getWiretype());
				aPWireEntity.setWirediam(bMaterialEntity.getWirediam());
				aPWireService.save(aPWireEntity);
			}
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody APWireEntity aPWire){
		aPWireService.update(aPWire);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		aPWireService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
